<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>景点管理</title>
    <!--引入vue.js库-->
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> -->
    <script src="/js/vue.js"></script>
    <!-- element-ui引入样式 展示数据的-->
    <link rel="stylesheet" href="/css/index.css">
    <!-- element-ui引入组件库 -->
    <script src="/js/index.js"></script>
    <!--引入axios库  从后端获取数据-->
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script>-->
    <script src="/js/axios.min.js"></script>

    <script src="/js/vue-qriously.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body>
<div id="app">
    <!--搜索-->
    <el-row :gutter="10">
        <!--一行分为4列-->
        <el-col :span="4">
            <el-input v-model="searchForm.name" size="mini" placeholder="请输入关键字"></el-input>
        </el-col>
        <el-col :span="6">
            <el-button type="success" icon="el-icon-search" size="mini" @click="search">搜索</el-button>
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">添加</el-button>
        </el-col>
        <el-col :span="14"></el-col>
    </el-row>
    <el-row>
        <el-col :span="24">
            &nbsp;
        </el-col>
    </el-row>
    <!--table 数据-->
    <el-table
            :data="tableData"
            style="width: 100%">
        <!--多表联查，显示景区名称-->
<!--        <el-table-column-->
<!--                prop="tmScenic.name"-->
<!--                label="景区名称"-->
<!--                width="180">-->
<!--        </el-table-column>-->
        <el-table-column
                prop="name"
                label="景点名称"
                width="180">
        </el-table-column>
        <el-table-column
                prop="sdot"
                label="景点图片"
                width="180">
            <template slot-scope="scope">
                <el-image
                        style="width: 150px; height: 115px"
                        :src="scope.row.sdot"
                ></el-image>
            </template>
        </el-table-column>
        <el-table-column
                prop="summary"
                label="简介"
                width="800">
        </el-table-column>
        <el-table-column fixed="right" width="200" label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑
                </el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index,scope.row)">删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <!--分页功能-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5,10,15,20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
    <!--添加或者更新的form表单-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form">

            <el-form-item label="景区名称" :label-width="formLabelWidth">
                <el-select v-model="value" clearable placeholder="请选择景区">
                    <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
                <!--                <el-input v-model="form.scenicid" autocomplete="off"></el-input>-->
            </el-form-item>
            <el-form-item label="景点名称" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="景点图片" :label-width="formLabelWidth">
                <!--上传组件-->
                <el-upload
                        style="width: 120px"
                        class="avatar-uploader"
                        action="/dot/uploadFile"
                        name="file2"
                        :show-file-list="false"
                        :on-success="handleFileSuccess"
                        :before-upload="beforeFileUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar" style="width: 220px">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                <!--  <el-input v-model="form.sdot" autocomplete="off"></el-input>-->
            </el-form-item>
            <el-form-item label="简介" :label-width="formLabelWidth">
                <el-input v-model="form.summary" autocomplete="off" type="textarea"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        </div>
    </el-dialog>
</div>
<script>
    //js代码
    var vue = new Vue({
        el: "#app",
        data() {
            return {//存放使用的所有属性数据
                tableData: [],//方便编写表单校验
                currentPage: 1,//页码 第几页
                pageSize: 5,//每页显示数量
                total: 100,//分页总数量
                searchForm: {name: '',scenicid:'',name:'',summary:''},//封装查询对象
                dialogFormVisible: false,//是否显示对话框
                title: '添加',//默认显示title
                form: {id:'',scenicid: '',sdot:'', name: '',summary:'',bgrId:'',delrId:'',delTime:''}, //form表单
                formLabelWidth: '120px',//labal宽度
                options:[],
                value:'',


                imageUrl:'',//图片回显Url地址

            }
        },
        created: function () {
            this.loadData();
        },
        methods: {
            loadData() {
                var _this = this;
                axios.post('/dot/queryPage',
                    {
                        "pageNo": this.currentPage,
                        "pageSize": this.pageSize,
                        "data":this.searchForm
                    }
                )
                    .then(function (response) {
                        console.log(response);
                        console.log(JSON.stringify(response));
                        if (response.data.code == 200) {
                            _this.tableData = response.data.data.list;
                            _this.total = response.data.data.total;
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
                //查询景区表，并将值赋给下拉框
                axios.get('/scenic/queryAll',//请求的url
                ).then(function (response) {//.then回调函数
                    console.log(JSON.stringify(response));
                    if (response.data.code == 200) {
                        _this.options = response.data.data; //给tableData属性赋值
                    }
                }).catch(function (error) {//异常处理
                    console.log(error);
                });
            },
            //每页数量发生变化
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val;
                //调用加载数据方法
                this.loadData();
            },
            //页码（第几页）发生变化
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                //调用加载数据方法
                this.loadData();
            },
            //搜索方法
            search() {
                this.loadData();
            },
            //跳转更新
            handleEdit(index, row) {
                console.log(index, row);
                //让对话框显示
                this.dialogFormVisible = true;
                //让title值也发生变化
                this.title = '更新';
                //给表单赋值
                this.form = row;
                //alert(JSON.stringify(row))
                //让数据回显
                this.value=row.tmScenic.id;
            },
            //跳转添加
            handleAdd() {
                var _this = this;
                //让对话框显示
                this.dialogFormVisible = true;
                //让title值也发生变化
                this.title = '添加';
                //让form清空
                this.form = {};
                //清空下拉框
                this.value='';
            },
            addOrUpdate() {
                var uri = "/dot/add";
                if (this.title == '更新') {
                    uri = "/dot/update";
                }
                var _this = this;
                //获取当前用户id
                axios.get("/user/getLoginSession",//请求的url
                ).then(function (response) {//.then回调函数
                    if (response.data.code == 200) {
                        _this.form.bgrId=response.data.data.userId
                        //拿到下拉框中的id赋给form对象中的scenicid属性
                        _this.form.scenicid=_this.value;
                        axios.post(uri, _this.form)
                            .then(function (response) {
                                //前端控制台打印  response 是json对象
                                console.log(response);
                                console.log(JSON.stringify(response));
                                if (response.data.code == 200) {
                                    //提示成功
                                    _this.$message({
                                        message: '恭喜你，这是一条成功消息',
                                        type: 'success'
                                    });
                                    //关闭弹框
                                    _this.dialogFormVisible = false;
                                    //重新加载数据
                                    _this.loadData();
                                } else {
                                    _this.$message.error('错了哦，这是一条错误消息');
                                }
                            })
                            .catch(function (error) {//异常处理
                                console.log(error);
                            });
                    } else {
                        _this.$message.error('错了哦，这是一条错误消息');
                    }
                }).catch(function (error) {//异常处理
                    console.log(error);
                });
            },
            //处理删除
            handleDelete(index, row) {
                console.log(index, row);
                //定义变量
                var _this = this;
                this.$confirm('确定删除该数据?', '提示', {
                    type: 'warning'
                }).then(() => {
                    axios.post('/tmScenicDot/queryScenicDotById/'+row.id
                    ).then(function (response) {
                        console.log(JSON.stringify(response));
                        if (response.data.code == 200) {
                            var id=response.data.data
                            axios.get("/user/getLoginSession",//请求的url
                            ).then(function (response) {//.then回调函数
                                console.log(JSON.stringify(response));
                                if (response.data.code == 200) {
                                    _this.form.delrId=response.data.data.userId
                                    _this.form.delTime=new Date().toLocaleString()
                                    axios.post("/tmScenicDot/update",//请求的url
                                        {"delState": 0, "id": id,"delrId":_this.form.delrId,"delTime":_this.form.delTime}
                                    ).then(function (response) {//.then回调函数
                                        if (response.data.code == 200) {
                                            axios.post('/dot/delete/' + row.id
                                            ).then(function (response) {
                                                if (response.data.code == 200) {
                                                    //提示成功
                                                    _this.$message({
                                                        message: '删除成功',
                                                        type: 'success'
                                                    });
                                                    //然后重新查询一遍数据
                                                    _this.loadData();
                                                } else {
                                                    _this.$message.error('删除失败');
                                                }
                                            }).catch((error) => {//异常处理
                                                console.log("删除失败的原因：", error)
                                            })
                                        } else {
                                            _this.$message.error('删除失败');
                                        }
                                    }).catch((error) => {//异常处理
                                        console.log(error);
                                    })
                                } else {
                                    _this.$message.error('错了哦，这是一条错误消息');
                                }
                            }).catch(function (error) {//异常处理
                                console.log(error);
                            });
                        }
                    })
                        .catch(function (error) {
                            console.log(error);
                        });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                })
            },

            //处理文件成功后的回调方法
            handleFileSuccess(res, file) {
                //回显上传图片
                this.imageUrl = URL.createObjectURL(file.raw);
                this.form.sdot = file.response.data.filePath;

            },
            //文件上传前的处理方法
            beforeFileUpload(file) {
                //   isJPG = file.type 赋值操作 把图片类型赋值给isJPG   ==  比较值 例如 1=='1' 为true
                //       ===  比较类型  比较值   1==='1'  为false  类型不等
                //const isJPG = file.type === 'image/jpeg';
                //   file.size文件大小，单位是字节
                const isLt5M = file.size / 1024 / 1024 < 5;

                /*if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }*/
                if (!isLt5M) {
                    this.$message.error('上传头像图片大小不能超过 5MB!');
                }
                //返回false不会执行
                return  isLt5M;
            }
        },
    })
</script>
</body>
</html>